package all

import (
	"github.com/gin-gonic/gin"
	"ruoyi/application"
	"ruoyi/infrastructure/persistence"
	"ruoyi/interfaces"
	"ruoyi/interfaces/middleware"
)

func InitTest(e *gin.Engine, s *persistence.SystemRepositories, m *persistence.MonitorRepositories) {

	captch := interfaces.NewCaptcha(s.Config)
	e.GET("/captchaImage", captch.CaptchaImageHandler)

	login := interfaces.NewLoginService(s.User, s.Config, s.Log, s.Menu, s.Role, s.Dept)
	e.POST("/login", login.LoginHandler)

	v1 := e.Group("/")
	{
		auth := v1.Group("")
		//限流测试
		//auth.Use(utils.LimitHandler(100))
		auth.Use(middleware.JWTAuthMiddleware())
		{
			auth.GET("getInfo", login.GetInfoHandler)
			auth.POST("logout", login.LogoutHandler)
			/*获取用户授权菜单*/
			auth.GET("getRouters", login.GetRoutersHandler)
		}
	}

	// 配置相关
	configGroup := e.Group("system/config")
	config := interfaces.NewConfig(s.Config, s.User)
	{
		auth := configGroup.Group("")
		auth.Use(middleware.JWTAuthMiddleware())
		{
			auth.GET("/list", config.ListConfig)
			auth.POST("/export", config.ExportConfig)
			auth.GET("/:configId", config.GetConfigInfo)
			auth.GET("/configKey/:configKey", config.GetConfigKey)
			auth.POST("", config.SaveConfig)
			auth.PUT("", config.UploadConfig)
			auth.DELETE("/:configIds", config.DetectConfig)
			auth.DELETE("/donws/:refreshCache", config.DeleteCacheConfig)
		}
	}

	// 消息相关
	noticeGroup := e.Group("system/notice")
	notice := interfaces.NewNotice(s.Notice, s.User)
	{
		auth := noticeGroup.Group("")
		auth.Use(middleware.JWTAuthMiddleware())
		{
			auth.GET("/list", notice.ListNotice)
			auth.GET("/:noticeId", notice.GetNotice)
			auth.POST("", notice.SaveNotice)
			auth.PUT("", notice.UploadNotice)
			auth.DELETE("/:noticeIds", notice.DeleteNotice)
		}
	}

	//字典相关
	dictGroup := e.Group("system/dict")
	dict := interfaces.NewDict(s.Dict, s.User, s.DictType)
	{
		auth := dictGroup.Group("")
		auth.Use(middleware.JWTAuthMiddleware())
		{
			// 字典数据
			auth.GET("/data/list", dict.ListDict)
			auth.POST("/data/export", dict.ExportDict)
			auth.GET("/data/:dictCode", dict.GetDictCode)
			auth.GET("/data/type/:dictType", dict.DictTypeHandler)
			auth.POST("/data", dict.SaveDictData)
			auth.PUT("/data", dict.UpDictData)
			auth.DELETE("/data/:dictCodes", dict.DeleteDictData)
			// 字典类型-字典管理
			auth.GET("/type/list", dict.ListDictType)
			auth.POST("/type/export", dict.ExportType)
			auth.GET("/type/:dictId", dict.GetTypeDict)
			auth.POST("/type", dict.SaveType)
			auth.PUT("/type", dict.UploadType)
			auth.DELETE("/type/:dictIds", dict.DeleteDataType)
			auth.DELETE("/refreshCache", dict.RefreshCache)
			auth.GET("/type/optionselect", dict.GetOptionSelect)
		}
	}

	// 部门相关
	deptGroup := e.Group("system/dept")
	dept := interfaces.NewDept(s.Dept, s.User)
	{
		auth := deptGroup.Group("")
		auth.Use(middleware.JWTAuthMiddleware())
		{
			// 部门数据
			auth.GET("/list", dept.ListDept)
			auth.GET("/list/exclude/:deptId", dept.ExcludeDept)
			auth.GET("/:deptId", dept.GetDept)
			auth.POST("", dept.SaveDept)
			auth.PUT("", dept.UpDataDept)
			auth.DELETE("/:deptId", dept.DeleteDept)
		}
	}

	// 菜单相关
	menuGroup := e.Group("system/menu")
	menu := interfaces.NewMenu(s.Menu, s.User, s.Role)
	{
		auth := menuGroup.Group("")
		auth.Use(middleware.JWTAuthMiddleware())
		{
			auth.GET("/list", menu.ListMenu)
			auth.GET("/:menuId", menu.GetMenuInfo)
			auth.GET("/treeselect", menu.GetTreeSelect)
			auth.GET("/roleMenuTreeselect/:roleId", menu.TreeSelectByRole)
			auth.POST("", menu.SaveMenu)
			auth.PUT("", menu.UploadMenu)
			auth.DELETE("/:menuId", menu.DeleteMenu)
		}
	}

	//岗位相关
	postGroup := e.Group("system/post")
	post := interfaces.NewPost(s.Post, s.User)
	{
		auth := postGroup.Group("")
		auth.Use(middleware.JWTAuthMiddleware())
		{
			auth.GET("/list", post.ListPost)
			auth.POST("/export", post.ExportPost)
			auth.GET("/:postId", post.GetPostInfo)
			auth.POST("", post.SavePost)
			auth.PUT("", post.UploadPost)
			auth.DELETE("/:postIds", post.DeletePost)
			auth.GET("/optionselect", post.GetPostOptionSelect)
		}
	}

	roleGroup := e.Group("system/role")
	role := interfaces.NewRole(s.Role, s.User, s.Dept)
	{
		auth := roleGroup.Group("")
		auth.Use(middleware.JWTAuthMiddleware())
		{
			auth.GET("/list", role.ListRole)
			auth.POST("/export", role.ExportRole)
			auth.GET("/:roleId", role.GetRoleInfo)
			auth.POST("", role.SaveRole)
			auth.PUT("", role.UploadRole)
			auth.PUT("/dataScope", role.PutDataScope)
			auth.PUT("/changeStatus", role.ChangeRoleStatus)
			auth.DELETE("/:roleIds", role.DeleteRole)
			auth.GET("/optionselect", role.GetRoleOptionSelect)
			auth.GET("/authUser/allocatedList", role.GetAllocatedList)
			auth.GET("/authUser/unallocatedList", role.GetUnAllocatedList)
			auth.PUT("/authUser/cancel", role.CancelRole)
			auth.PUT("/authUser/cancelAll", role.CancelAllRole)
			auth.PUT("/authUser/selectAll", role.SelectRoleAll)
			auth.GET("/deptTree/:roleId", role.GetDeptTreeRole)
		}
	}

	userGroup := e.Group("system")
	user := interfaces.NewUser(s.Role, s.User, s.Dept, s.Post)
	{
		auth := userGroup.Group("")
		auth.Use(middleware.JWTAuthMiddleware())
		{
			auth.GET("/user/list", user.ListUser)
			auth.POST("/user/export", user.ExportExport)
			auth.POST("/user/importData", user.ImportUserData)
			auth.POST("/user/importTemplate", user.ImportTemplate)
			auth.GET("/user/:userId", user.GetUserInfo)
			auth.GET("/user/", user.GetUserInfo)
			auth.POST("/user", user.SaveUser)
			auth.PUT("/user", user.UploadUser)
			auth.DELETE("/user/:userIds", user.DeleteUserById)
			auth.PUT("/user/resetPwd", user.ResetPwd)
			auth.PUT("/user/changeStatus", user.ChangeUserStatus)
			auth.GET("/user/authRole/:userId", user.GetAuthUserRole)
			auth.PUT("/user/authRole", user.PutAuthUser)
			auth.GET("/user/deptTree", user.GetUserDeptTree)
		}
	}

	// 服务监控
	serviceGroup := e.Group("monitor")
	service := interfaces.NewService(&application.ServiceApp{}, m.Operation)
	{
		auth := serviceGroup.Group("")
		auth.Use(middleware.JWTAuthMiddleware())
		{
			auth.GET("/server", service.ServerData)
		}
	}

	// 操作日志相关
	operationGroup := e.Group("monitor/operlog")
	operation := interfaces.NewOperationLog(m.Operation)
	{
		auth := operationGroup.Group("")
		auth.Use(middleware.JWTAuthMiddleware())
		{
			auth.GET("/list", operation.ListOperationLog)
			auth.DELETE("/:operId", operation.DelectOperationLog)
			auth.DELETE("/clean", operation.ClearOperationLog)
			auth.POST("/export", operation.ExportOperationLog)
		}
	}

	// 定时任务相关
	jobGroup := e.Group("monitor/job")
	job := interfaces.NewJob(m.Job)
	{
		auth := jobGroup.Group("")
		auth.Use(middleware.JWTAuthMiddleware())
		{
			auth.GET("list", job.ListJob)
			auth.POST("export", job.ExportJob)
			auth.GET(":jobId", job.GetJobById)
			auth.POST("", job.SaveJob)
			auth.PUT("", job.UploadJob)
			auth.PUT("changeStatus", job.ChangeStatus)
			auth.PUT("run", job.RunJob)
			auth.DELETE(":jobIds", job.DeleteJob)
		}
	}

	// 在线用户相关
	onlineGroup := e.Group("monitor/online")
	online := interfaces.NewOnline(m.Operation)
	{
		auth := onlineGroup.Group("")
		auth.Use(middleware.JWTAuthMiddleware())
		{
			auth.GET("list", online.ListOnLine)
			auth.DELETE("/:tokenId", online.DeleteOnLine)
		}
	}

	// 登录日志相关
	logGroup := e.Group("monitor/logininfor")
	log := interfaces.NewLog(s.Log)
	{
		auth := logGroup.Group("")
		auth.Use(middleware.JWTAuthMiddleware())
		{
			auth.GET("/list", log.LoginInformListHandler)
			auth.POST("/export", log.ExportHandler)
			auth.DELETE("/:infoIds", log.DeleteByIdHandler)
			auth.DELETE("/clean", log.CleanHandler)
			auth.GET("/unlock/:userName", log.UnlockHandler)
		}
	}

	// 缓存相关
	cacheGroup := e.Group("monitor/cache")
	cache := interfaces.NewCache()
	{
		auth := cacheGroup.Group("")
		auth.Use(middleware.JWTAuthMiddleware())
		{
			auth.GET("", cache.CacheHandler)
			auth.GET("getNames", cache.CacheHandler)
			auth.GET("getKeys/:cacheName", cache.GetCacheKeysHandler)
			auth.GET("getValue/:cacheName/:cacheKey", cache.GetCacheValueHandler)
			auth.DELETE("clearCacheName/:cacheName", cache.ClearCacheNameHandler)
			auth.DELETE("clearCacheKey/:cacheKey", cache.ClearCacheKeyHandler)
			auth.DELETE("clearCacheAll", cache.ClearCacheAllHandler)
		}
	}
}
